package thread.threaddemo;

/**
 * @Author: wenjingyuan
 * @Date: 2022/11/18/17:46
 * @Description:Synchronized修饰普通方法
 */
public class ThreadSynchronized2 {
    private  static int number=0;
    //定义一个内部类
    static class Counter{
        private static int MAX_Counter=100000;

        public synchronized  void incr(){
            for (int i = 0; i < MAX_Counter; i++) {
                number++;
            }
        }
        public synchronized void decr(){
            for (int i = 0; i < MAX_Counter; i++) {
                number--;
            }

        }
    }

    public static void main(String[] args) throws InterruptedException {
        Counter counter=new Counter();
        Thread t1=new Thread(()->{
            counter.incr();
        });
        t1.start();
        Thread t2=new Thread(()->{
            counter.decr();
        });
        t2.start();

        t1.join();
        t2.join();

        System.out.println("最终的结果"+ number);
    }
}
